import { assert, assertEquals } from "asserts";

import exchange from "./index.ts";

Deno.test(
    "diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof",
    () => {
        const inputs = [
            [1, 2, 3, 4],
            [
                2856,
                3249,
                3425,
                989,
                41,
                1027,
                2591,
                4979,
                1645,
                768,
                4602,
                4452,
                1681,
                3373,
                4273,
                384,
                3792,
                1662,
                3026,
                1841,
                3653,
                2713,
                1833,
                39,
                524,
                3675,
                3580,
                1488,
                2259,
                3845,
                2550,
                769,
                620,
                2314,
                4223,
                717,
                1930,
                1426,
                2285,
                3025,
                1398,
                2719,
                1995,
                3801,
                3115,
                1245,
                212,
                3857,
                3631,
                3475,
                4583,
                4811,
                2127,
                314,
                4357,
                1145,
                3643,
                3991,
                3676,
                1239,
                141,
                1559,
                470,
                1023,
                3581,
                601,
                2075,
                1404,
                1344,
                4592,
                3081,
                333,
                2222,
                3260,
                3952,
                774,
                3559,
                2921,
                1209,
                1205,
                2139,
                2810,
                890,
                186,
                2013,
                1343,
                507,
                1031,
                3029,
                1523,
                321,
                4717,
                68,
                1095,
                3931,
                2762,
                1205,
                3424,
                3465,
                3257,
                3179,
                4141,
                1549,
                1965,
                3053,
                588,
                4874,
                1661,
                3188,
                2483,
                4882,
                444,
                2197,
                896,
                4531,
                1554,
                1670,
                2675,
                3325,
                522,
                3747,
                2359,
                4702,
                2600,
                4555,
                1687,
                493,
                3637,
                2150,
                3322,
                2921,
                2891,
                3023,
                4812,
                342,
                657,
                299,
                2655,
                3810,
                1979,
                4315,
                833,
                209,
                2090,
                4687,
                4193,
                1154,
                2506,
                3747,
                4862,
                1454,
                2187,
                3372,
                4610,
                3552,
                4222,
                605,
                2542,
                3664,
                4490,
                4812,
                3997,
                4543,
                455,
                1842,
                48,
                2483,
                2647,
                349,
                1501,
                1414,
                237,
                2328,
                728,
                2331,
                4566,
                997,
                3218,
                2802,
                4164,
                1858,
                4591,
                1542,
                1684,
                932,
                2063,
                754,
                1578,
                2517,
                4795,
                3040,
                3575,
                768,
                1488,
                3033,
                2523,
                2269,
                255,
                4605,
                1605,
                111,
                4450,
                1192,
                2701,
                1485,
                1469,
                1962,
                3360,
                69,
                3433,
                130,
                728,
                2047,
                395,
                1425,
                2741,
                1073,
                2152,
                3300,
                915,
                752,
                4093,
                2395,
                4750,
                883,
                1157,
                3309,
                215,
                2762,
                1250,
                4223,
                2070,
                1630,
                4600,
                4956,
                2172,
                1017,
                2633,
                914,
                4187,
                2842,
                3754,
                4366,
                1370,
                4609,
                4438,
                60,
                4552,
                316,
                4588,
                4351,
                885,
                1225,
                3562,
                4263,
                2205,
                1697,
                545,
                1022,
                269,
                3239,
                245,
                3913,
                781,
                4047,
                2872,
                1445,
                1224,
                4366,
                2619,
                3111,
                732,
                4915,
                3058,
                4909,
                3860,
                380,
                193,
                4461,
                2337,
                924,
                4042,
                2003,
                104,
                2799,
                4536,
                4316,
                4151,
                259,
                4085,
                765,
                683,
                930,
                1015,
                4994,
                1231,
                3616,
                3207,
                3303,
                1883,
                2757,
                4690,
                4860,
                2690,
                2583,
                4556,
                4627,
                3847,
                3635,
                3731,
                4722,
                3980,
                4190,
                1350,
                2719,
                2618,
                4751,
                4933,
                3007,
                2320,
                4866,
                2190,
                4763,
                3754,
                3894,
                3968,
                4600,
                3792,
                1838,
                1499,
                2786,
                2274,
                323,
                3607,
                1769,
                405,
                988,
                2632,
                913,
                164,
                1590,
                2194,
                2925,
                1397,
                4885,
                879,
                3701,
                4946,
                2070,
                4273,
                3143,
                4187,
                539,
                4660,
                986,
                3367,
                4415,
                3291,
                2937,
                4069,
                3461,
                863,
                2707,
                3317,
                3979,
                4239,
                3363,
                4321,
                1803,
                1918,
                542,
                2286,
                2703,
                3169,
                1722,
                4050,
                1506,
                1238,
                200,
                4557,
                2995,
                2296,
                585,
                335,
                4205,
                332,
                325,
                1871,
                633,
                898,
                662,
                2923,
                4471,
                2191,
                1474,
                2544,
                4741,
                707,
                188,
                1362,
                3816,
                289,
                4259,
                631,
                3779,
                3167,
                3630,
                2297,
                58,
                3968,
                1968,
                4072,
                3161,
                2691,
                2471,
                1276,
                2968,
                561,
                8,
                968,
                916,
                1407,
                2372,
                2636,
                2527,
                2373,
                615,
                0,
                1028,
                4172,
                3192,
                540,
                204,
                2303,
                4333,
                638,
                250,
                4922,
                1098,
                3758,
                4153,
                1704,
                2638,
                3786,
                3189,
                664,
                2465,
                3144,
                2416,
                14,
                2356,
                3510,
                3749,
                435,
                1642,
                430,
                672,
                4238,
                722,
                2652,
                1160,
                2541,
                2616,
                4067,
                2110,
                555,
                659,
                752,
                4930,
                3564,
                1387,
                4526,
                495,
                877,
                31,
                1351,
                1271,
                2214,
                1398,
                3150,
                4719,
                2928,
                657,
                710,
                892,
                677,
                2440,
                3855,
                725,
                2112,
                2210,
                4028,
                2366,
                1688,
                3448,
                2375,
                3760,
                1213,
                4635,
                4937,
                4320,
                3308,
                4703,
                640,
                4986,
                701,
                2809,
                234,
                3123,
                3122,
                1309,
                837,
                2923,
                4786,
                2718,
                2018,
                3952,
                3706,
                873,
                1908,
                3338,
                1128,
                4206,
                20,
                113,
                3746,
                1525,
                4707,
                1253,
                4041,
                3590,
                3265,
                71,
                1864,
                4971,
                807,
                1380,
                4023,
                667,
                4915,
                1708,
                3024,
                1716,
                2568,
                3537,
                1951,
                1003,
                1925,
                4710,
                4003,
                2064,
                1462,
                4314,
                4795,
                1134,
                1706,
                589,
                3641,
                3819,
                251,
                2408,
                4748,
                174,
                2777,
                1470,
                4858,
                1921,
                2866,
                4506,
                3884,
                1533,
                1058,
                3131,
                2162,
                3942,
                1979,
                2943,
                4221,
                2099,
                280,
                4704,
                2667,
                4467,
                1149,
                2583,
                19,
                2336,
                2462,
                3770,
                2345,
                2931,
                77,
                1744,
                95,
                2323,
                836,
                1553,
                451,
                1118,
                4229,
                4379,
                1228,
                4423,
                2630,
                3931,
                1121,
                2737,
                3185,
                269,
                782,
                766,
                2793,
                4033,
                1705,
                4731,
                739,
                1453,
                246,
                1786,
                2181,
                804,
                1633,
                3291,
                1311,
                515,
                1810,
                2744,
                3796,
                4356,
                1030,
                742,
                410,
                3141,
                2453,
                1962,
                3218,
                69,
                4766,
                543,
                1350,
                498,
                1467,
                101,
                2684,
                489,
                1804,
                1338,
                2049,
                665,
                2452,
                1303,
                4753,
                1381,
                4806,
                3910,
                3455,
                1787,
                3755,
                2832,
                184,
                3089,
                3314,
                2208,
                664,
                2100,
                4679,
                172,
                2468,
                734,
                624,
                1400,
                2570,
                4475,
                4481,
                1906,
                93,
                4784,
                1732,
                1121,
                1294,
                2086,
                3289,
                3489,
                2116,
                2494,
                2658,
                4542,
                4243,
                3010,
                433,
                3507,
                2167,
                3135,
                4492,
                1809,
                1794,
                973,
                2617,
                2759,
                4245,
                699,
                1025,
                1243,
                2770,
                3070,
                4117,
                3744,
                494,
                2783,
                1882,
                1672,
                928,
                4958,
                4247,
                1708,
                3399,
                956,
                4404,
                3120,
                3933,
                3910,
                735,
                928,
                4440,
                4750,
                1285,
                2784,
                4504,
                2699,
                2972,
                1466,
                1260,
                97,
                2800,
                912,
                356,
                424,
                4852,
                2838,
                1235,
                4224,
                695,
                2997,
                676,
                4355,
                439,
                835,
                1965,
                444,
                2057,
                3925,
                1249,
                1705,
                4264,
                975,
                759,
                1088,
                4363,
                2544,
                4965,
                3871,
                2907,
                4215,
                3090,
                297,
                1069,
                1373,
                2241,
                3208,
                1228,
                3951,
                3869,
                4919,
                2503,
                256,
                406,
                4860,
                2489,
                2951,
                1824,
                4052,
                3325,
                4746,
                2639,
                4777,
                1745,
                59,
                402,
                657,
                4349,
                674,
                4202,
                2401,
                1210,
                256,
                2732,
                4319,
                4307,
                3944,
                2817,
                3052,
                74,
                1879,
                3607,
                2524,
                1661,
                1424,
                2114,
                4854,
                906,
                2409,
                2980,
                1651,
                603,
                4586,
                3141,
                2897,
                4486,
                4628,
                2240,
                1222,
                958,
                3928,
                267,
                3910,
                4131,
                3016,
                3866,
                2791,
                3162,
                200,
                2635,
                1401,
                3420,
                3102,
                4466,
                4633,
                445,
                776,
                819,
                365,
                1585,
                3308,
                4831,
                3619,
                1370,
                741,
                673,
                12,
                3236,
                1860,
                3592,
                2002,
                3675,
                1490,
                4333,
                549,
                1070,
                4512,
                2787,
                2694,
                950,
                3923,
                3859,
                2544,
                3390,
                177,
                1406,
                2847,
                2371,
                2975,
                3510,
                583,
                2613,
                2290,
                4408,
                3884,
                2583,
                2427,
                3142,
                4039,
                1964,
                2224,
                1036,
                1992,
                4409,
                2919,
                4689,
                1163,
                1991,
                4893,
                4743,
                4563,
                2184,
                4094,
                3693,
                3346,
                2919,
                1679,
                3588,
                2712,
                4358,
                2201,
                844,
                3968,
                781,
                2579,
                1795,
                3291,
                3159,
                4446,
                3713,
                4549,
                1435,
                4611,
                2269,
                4813,
                1417,
                1149,
                4541,
                4853,
                2571,
                1640,
                4503,
                3097,
                1298,
                2922,
                3297,
                277,
                1027,
                4135,
                4248,
                3305,
                2469,
                3756,
                2979,
                1521,
                2392,
                1612,
                4932,
                1880,
                2718,
                363,
                3047,
                1824,
                333,
                1068,
                4507,
                1095,
                1477,
                585,
                3904,
                2202,
                3050,
                3646,
                1835,
                4695,
                3472,
                3436,
                3700,
                1424,
                2108,
                1486,
                1506,
                3352,
                3516,
                4789,
                3966,
                3222,
                112,
                3507,
                3219,
                3287,
                4442,
                4820,
                4418,
                2442,
                1398,
                517,
                3962,
                107,
                2803,
                3539,
                4970,
                413,
                4303,
                2347,
                452,
                648,
                1999,
                671,
                4939,
                1227,
                2454,
                1178,
                604,
                4895,
                1819,
                2115,
                2979,
            ],
        ];
        const outputs = [
            [1, 3, 2, 4],
            [
                4221,
                589,
                3641,
                3819,
                251,
                2777,
                1921,
                1533,
                3131,
                1979,
                2943,
                4795,
                2099,
                2667,
                4467,
                1149,
                2583,
                19,
                2345,
                2931,
                77,
                4971,
                837,
                2923,
                873,
                113,
                1525,
                4707,
                1253,
                4041,
                3265,
                71,
                95,
                807,
                4023,
                667,
                4915,
                3537,
                1951,
                1003,
                1925,
                4003,
                1303,
                515,
                3141,
                2453,
                69,
                543,
                1467,
                101,
                489,
                2049,
                665,
                1311,
                4753,
                1381,
                3455,
                1787,
                3755,
                3089,
                4679,
                4475,
                4481,
                269,
                2323,
                1553,
                451,
                4229,
                4379,
                4423,
                3931,
                1121,
                2737,
                3185,
                1309,
                2793,
                4033,
                1705,
                4731,
                739,
                1453,
                2181,
                1633,
                3291,
                325,
                3363,
                4321,
                1803,
                2703,
                3169,
                4557,
                2995,
                585,
                335,
                4205,
                4239,
                1871,
                633,
                2923,
                4471,
                2191,
                4741,
                707,
                289,
                4259,
                3367,
                913,
                2925,
                1397,
                4885,
                879,
                3701,
                4273,
                3143,
                4187,
                539,
                631,
                4415,
                3291,
                2937,
                4069,
                3461,
                863,
                2707,
                3317,
                3979,
                3855,
                659,
                1387,
                495,
                877,
                31,
                1351,
                1271,
                4719,
                657,
                677,
                555,
                725,
                2375,
                1213,
                4635,
                4937,
                4703,
                701,
                2809,
                3123,
                615,
                3779,
                3167,
                2297,
                3161,
                2691,
                2471,
                561,
                1407,
                2527,
                2373,
                93,
                2303,
                4333,
                4153,
                3189,
                2465,
                3749,
                435,
                2541,
                4067,
                1795,
                1991,
                4893,
                4743,
                4563,
                3693,
                2919,
                1679,
                2201,
                781,
                2579,
                1163,
                3291,
                3159,
                3713,
                4549,
                1435,
                4611,
                2269,
                4813,
                1417,
                2371,
                741,
                673,
                3675,
                4333,
                549,
                2787,
                3923,
                3859,
                177,
                2847,
                1149,
                2975,
                583,
                2613,
                2583,
                2427,
                4039,
                4409,
                2919,
                4689,
                4303,
                4695,
                4789,
                3507,
                3219,
                3287,
                517,
                107,
                2803,
                3539,
                413,
                1835,
                2347,
                1999,
                671,
                4939,
                1227,
                4895,
                1819,
                2115,
                2979,
                2469,
                4541,
                4853,
                2571,
                4503,
                3097,
                3297,
                277,
                1027,
                4135,
                3305,
                3619,
                2979,
                1521,
                363,
                3047,
                333,
                4507,
                1095,
                1477,
                585,
                1965,
                735,
                1285,
                2699,
                97,
                1235,
                695,
                2997,
                4355,
                439,
                835,
                3933,
                2057,
                3925,
                1249,
                1705,
                975,
                759,
                4363,
                4965,
                3871,
                2617,
                1121,
                3289,
                3489,
                4243,
                433,
                3507,
                2167,
                3135,
                1809,
                973,
                2907,
                2759,
                4245,
                699,
                1025,
                1243,
                4117,
                2783,
                4247,
                3399,
                4131,
                2817,
                1879,
                3607,
                1661,
                2409,
                1651,
                603,
                3141,
                2897,
                267,
                4307,
                2791,
                2635,
                1401,
                4633,
                445,
                819,
                365,
                1585,
                4831,
                2951,
                4215,
                297,
                1069,
                1373,
                2241,
                3951,
                3869,
                4919,
                2503,
                2489,
                3249,
                3325,
                2639,
                4777,
                1745,
                59,
                657,
                4349,
                2401,
                4319,
                4223,
                3559,
                2921,
                4609,
                1205,
                2139,
                4187,
                2633,
                1017,
                2013,
                1343,
                507,
                1031,
                3029,
                1523,
                321,
                333,
                4717,
                215,
                3309,
                1095,
                1157,
                3931,
                883,
                2395,
                4093,
                915,
                1205,
                1073,
                2741,
                1425,
                3465,
                3913,
                4357,
                4909,
                4915,
                1145,
                3643,
                3111,
                2619,
                3991,
                1445,
                1239,
                141,
                4047,
                1559,
                781,
                1023,
                3257,
                245,
                3581,
                601,
                2075,
                3239,
                269,
                545,
                1697,
                2205,
                4263,
                3081,
                1225,
                885,
                4351,
                2647,
                997,
                2359,
                2331,
                4555,
                1687,
                493,
                3637,
                237,
                2921,
                2891,
                3023,
                1501,
                657,
                349,
                299,
                3747,
                2655,
                2483,
                1979,
                4315,
                833,
                209,
                455,
                4543,
                3997,
                605,
                4687,
                2187,
                4193,
                3747,
                255,
                395,
                2047,
                3179,
                3433,
                69,
                4141,
                1469,
                1485,
                1549,
                2701,
                1965,
                111,
                1605,
                3053,
                4605,
                1209,
                2269,
                2523,
                3033,
                3575,
                4795,
                2517,
                1661,
                2483,
                2197,
                2063,
                4531,
                4591,
                2675,
                3325,
                2003,
                1015,
                1645,
                4979,
                2591,
                1499,
                1027,
                1231,
                769,
                2713,
                3845,
                3653,
                4223,
                717,
                323,
                3607,
                1769,
                3207,
                3303,
                4751,
                4085,
                259,
                765,
                3675,
                4151,
                2719,
                4273,
                2259,
                39,
                2127,
                4933,
                3007,
                3373,
                1681,
                1833,
                683,
                4763,
                2799,
                3631,
                2719,
                2757,
                1995,
                3801,
                4627,
                3847,
                3115,
                3475,
                1841,
                1245,
                3857,
                3425,
                405,
                3635,
                3731,
                193,
                4461,
                4811,
                2337,
                4583,
                3025,
                989,
                2583,
                41,
                2285,
                1883,
                3052,
                2856,
                2442,
                406,
                1824,
                4860,
                3966,
                1554,
                1670,
                896,
                3188,
                1210,
                3222,
                256,
                2732,
                4874,
                3026,
                1662,
                4202,
                588,
                674,
                4052,
                3792,
                402,
                4882,
                444,
                4442,
                112,
                4746,
                4820,
                4418,
                3944,
                1466,
                2454,
                1178,
                4224,
                604,
                2838,
                4852,
                424,
                356,
                912,
                2800,
                2090,
                1260,
                676,
                2972,
                4504,
                2784,
                4750,
                4440,
                928,
                1154,
                3910,
                2506,
                3120,
                4404,
                314,
                4970,
                1398,
                384,
                522,
                3962,
                1228,
                3208,
                4702,
                2600,
                3090,
                2150,
                2544,
                3322,
                256,
                1088,
                4452,
                4264,
                4602,
                4812,
                342,
                768,
                452,
                648,
                444,
                3810,
                3756,
                4408,
                2290,
                2314,
                4248,
                620,
                3510,
                2222,
                3260,
                3952,
                1406,
                774,
                3390,
                2544,
                3884,
                2550,
                950,
                2694,
                734,
                4512,
                1070,
                2392,
                1490,
                2810,
                2002,
                3592,
                1860,
                1640,
                3968,
                844,
                4446,
                4358,
                2712,
                3588,
                212,
                3346,
                3676,
                4094,
                2184,
                470,
                1398,
                1612,
                1992,
                1036,
                2224,
                1426,
                1964,
                1298,
                1404,
                2922,
                3142,
                1344,
                1930,
                4592,
                3436,
                958,
                1222,
                3904,
                2202,
                2240,
                4628,
                4486,
                3050,
                3424,
                3646,
                4586,
                3472,
                2980,
                3928,
                906,
                3700,
                4854,
                2114,
                1424,
                1424,
                2524,
                2108,
                1486,
                1506,
                3352,
                74,
                3580,
                3236,
                4932,
                12,
                890,
                186,
                1370,
                1880,
                2718,
                3308,
                1488,
                1824,
                776,
                3516,
                1068,
                4466,
                3102,
                3420,
                68,
                200,
                3162,
                524,
                3866,
                3016,
                3910,
                2762,
                752,
                4238,
                722,
                2652,
                1160,
                3860,
                2616,
                2110,
                3058,
                672,
                4930,
                3564,
                732,
                4526,
                4366,
                1224,
                2214,
                1398,
                664,
                250,
                4922,
                1098,
                3758,
                924,
                1704,
                2638,
                3786,
                3150,
                3144,
                2416,
                14,
                2356,
                3510,
                380,
                1642,
                430,
                4588,
                234,
                3562,
                3122,
                4786,
                2718,
                2018,
                3952,
                3706,
                4986,
                1908,
                3338,
                1128,
                4206,
                20,
                316,
                3746,
                4552,
                2366,
                2872,
                2928,
                710,
                892,
                2440,
                2112,
                2210,
                4028,
                638,
                1688,
                3448,
                3760,
                1022,
                4320,
                3308,
                640,
                4722,
                2320,
                2618,
                1350,
                4190,
                1918,
                542,
                2286,
                3980,
                4866,
                1722,
                4050,
                1506,
                1238,
                200,
                2296,
                4556,
                332,
                1838,
                2632,
                164,
                1590,
                2194,
                2274,
                2786,
                4946,
                2070,
                2690,
                3792,
                4600,
                4660,
                986,
                3968,
                3894,
                3754,
                2190,
                4536,
                1276,
                2968,
                8,
                968,
                916,
                4316,
                2372,
                2636,
                4072,
                104,
                0,
                1028,
                4172,
                3192,
                540,
                204,
                4042,
                3816,
                4860,
                898,
                662,
                4690,
                1474,
                2544,
                188,
                1362,
                60,
                3616,
                4994,
                930,
                3630,
                58,
                3968,
                1968,
                3314,
                3218,
                4806,
                3910,
                4566,
                728,
                2832,
                184,
                2328,
                2802,
                2208,
                664,
                2100,
                172,
                2468,
                988,
                624,
                1400,
                498,
                410,
                1578,
                754,
                1962,
                3218,
                4766,
                932,
                1350,
                2570,
                1684,
                1542,
                2684,
                1804,
                1338,
                1858,
                4164,
                2452,
                3744,
                3664,
                2542,
                4222,
                3552,
                4610,
                3372,
                2770,
                3070,
                1794,
                494,
                1454,
                1882,
                1672,
                928,
                4958,
                4862,
                1708,
                2494,
                1414,
                1906,
                4784,
                1732,
                1294,
                2086,
                48,
                2116,
                742,
                2658,
                4542,
                1842,
                3010,
                4812,
                4492,
                4490,
                2408,
                2064,
                1462,
                4314,
                2070,
                1134,
                1706,
                1250,
                2762,
                1630,
                4748,
                174,
                1470,
                4858,
                2866,
                4506,
                3884,
                1058,
                914,
                4438,
                3590,
                1370,
                4366,
                1864,
                3754,
                2842,
                1380,
                4750,
                1708,
                3024,
                1716,
                2568,
                2172,
                4956,
                4600,
                4710,
                1786,
                1228,
                2630,
                1192,
                4450,
                782,
                766,
                1488,
                246,
                1962,
                768,
                804,
                3040,
                1810,
                2744,
                3796,
                4356,
                1030,
                2462,
                2162,
                3942,
                752,
                280,
                4704,
                3300,
                2152,
                2336,
                956,
                3770,
                728,
                1744,
                130,
                836,
                3360,
                1118,
            ],
        ];

        assert(
            inputs
                .map(exchange)
                .every((result, index) => {
                    const out = outputs[index] as number[];
                    const mid = out.findIndex((x) => x % 2 === 0);
                    const left1 = new Set(result.slice(0, mid));
                    const left2 = new Set(out.slice(0, mid));
                    // console.log(left1,left2)
                    assertEquals(
                        left1,
                        left2,
                    );
                    assertEquals(
                        new Set(result.slice(mid)),
                        new Set(out.slice(mid)),
                    );
                    return out.length === result.length;
                }),
        );
    },
);
